/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __IMAGE_H__
#define __IMAGE_H__

#include <boost/serialization/is_abstract.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/string.hpp>
#include "color.h"

#include <string>

namespace Sirat {

namespace Common {
	
/**
 * Image base class.
 */

class Image {
public:
	
	/**
 	 * Virtual Destructor.
	 */
	virtual ~Image();
	
	/**
	 * Save an image.
	 * @param _file Filename
	 */
	virtual void save_file(const char* _file) = 0;
	
	/**
	 * Load an image.
	 * @param _file Filename
	 */
	virtual void load_file(const char* _file) = 0;
	
	/**
	 * Save a color in a pixel.
	 * @param _px X coordinate
	 * @param _py Y coordinate
	 * @param _color Pixel's color
	 */
	void set_pixel(const unsigned int _px,const unsigned int _py,const Color& _color);

	/**
	 * Get pixel color.
	 * @param _px X coordinate
	 * @param _py Y coordinate
	 * @return _color Pixel's color
	 */
	Color get_pixel(const unsigned int _px,const unsigned int _py);
			
	/**
	 * @return Current width.
	 */
	unsigned int get_width() const;
	
	/**
	 * @return Current height.
	 */
	unsigned int get_height() const;
		
	/**
	 * @return Current filename
	 */
	std::string get_filename() const;
	
	/**
	 * Change current width.
	 * @param  _width New width.
	 */
	void set_width(const unsigned int _width);
	
	/**
	 * Change current height.
	 * @param  _height New height
	 */
	void set_height(const unsigned int _height);	

	/**
	 * ???
	 */
	bool loaded() const;
	
protected:
	/**
	 * Simple Constructor.
	 * @param _width 
	 * @param _height 
	 * @see Image(Color** _pixels,const unsigned int _width,const unsigned int _height);
	 */
	Image(unsigned int _width,unsigned int _height);
	
	/**
	 * Default Constructor.
	 * @see Image(const unsigned int _width,const unsigned int _height);
	 */
	Image();
	
	/**
	 * Constructor based on an pre-existent pixels matrix.
	 * @param _pixels
	 * @param _width 
	 * @param _height 
	 * @see Image(unsigned int _width,unsigned int _height);
	 */
	Image(Color** _pixels,const unsigned int _width,const unsigned int _height);
	
	/**
	 * Copy constructor
	 * @param _output 
	 * @see Image(unsigned int _width,unsigned int _height);
	 */
	Image(const Image& _output);
	
	void initiate_pixels(const unsigned int _width, const unsigned int _height);
	
	Image& operator=(const Image& _image);
	
	/**
	 * This operator returns Color** because Image is an abstract class.
	 * @param _output 
	 * @return Result pixels
	 */
	Color** operator+(const Image* const _output) const;
	
	unsigned int width,height;	
	Color** pixels;
	
	std::string filename;
	bool load;
	
private:
	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);
};



template<class Archive> void Image::serialize(Archive& ar, const unsigned int /* file_version */){
	ar  & BOOST_SERIALIZATION_NVP(filename)
		& BOOST_SERIALIZATION_NVP(width)
		& BOOST_SERIALIZATION_NVP(height);
}

}

}

BOOST_IS_ABSTRACT(Sirat::Common::Image)

#endif
